package zk.weart.util.file;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.*;

/**
 * @Author zjk
 * @Description: 文件 转换
 * @Date 2022/5/12 14:22
 **/
public class FileByteTransUtils {


  public   static void ByteToFile(byte[] bytes,String filePath, String fileType)throws Exception{
        ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
        BufferedImage bi1 = ImageIO.read(bais);
        try {
            File w2 = new File(filePath);
            ImageIO.write(bi1, fileType, w2);
        } catch (IOException e) {
            e.printStackTrace();
        }finally{
            bais.close();
        }
    }

    public static final byte[] input2byte(InputStream inStream)
            throws IOException {
        ByteArrayOutputStream swapStream = new ByteArrayOutputStream();
        byte[] buff = new byte[100];
        int rc = 0;
        while ((rc = inStream.read(buff, 0, 100)) > 0) {
            swapStream.write(buff, 0, rc);
        }
        byte[] in2b = swapStream.toByteArray();
        return in2b;
    }

    /**
     * 以下是byte  和inputStream  互相转换
     *
     * @param buf
     * @return
     */
    public static final InputStream byte2Input(byte[] buf) {

        return new ByteArrayInputStream(buf);
    }
}
